package com.situ.car.dao.Impl;

import com.situ.car.dao.IOrderDao;
import com.situ.car.pojo.entity.Order;
import com.situ.car.pojo.query.OrderQuery;
import com.situ.car.pojo.vo.OrderCountVo;
import com.situ.car.pojo.vo.OrderVo;
import com.situ.car.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrdorDaoImpl implements IOrderDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public int selectTotalCount(OrderQuery orderQuery) {
        String sql = "select count(*) \n" +
                "FROM rental_order AS r\n" +
                "JOIN car AS c ON r.car_id = c.id\n" +
                "JOIN user AS u ON r.user_id = u.id where 1=1";
        List<Object> queryList = new ArrayList<>();
        Integer userId = orderQuery.getUserId();
        sql += " and u.id = ?";
        queryList.add(userId);
        String queryModel = orderQuery.getModel();
        if (!ObjectUtils.isEmpty(queryModel)) {
            sql += " and c.model like ?";
            queryList.add("%" + queryModel + "%");
        }
        Integer count = template.queryForObject(sql, Integer.class, queryList.toArray());
        return count;
    }

    @Override
    public List<OrderVo> selectByPage(OrderQuery orderQuery) {
        String sql = "SELECT \n" +
                "    r.id AS id,\n" +
                "    r.order_number AS orderNumber,\n" +
                "    c.model AS model,\n" +
                "    c.image AS image,\n" +
                "    c.license_plate AS licensePlate,\n" +
                "    u.username AS username,\n" +
                "    r.rental_start_date AS rentalStartDate,\n" +
                "    r.rental_end_date AS rentalEndDate,\n" +
                "    r.order_status AS orderStatus,\n" +
                "    r.total_cost AS totalCost,\n" +
                "    r.order_time AS orderTime\n" +
                "FROM rental_order AS r\n" +
                "JOIN car AS c ON r.car_id = c.id\n" +
                "JOIN user AS u ON r.user_id = u.id where 1=1";
        List<Object> queryList = new ArrayList<>();
        Integer userId = orderQuery.getUserId();
        sql += " and u.id = ?";
        queryList.add(userId);
        String queryModel = orderQuery.getModel();
        if (!ObjectUtils.isEmpty(queryModel)) {
            sql += " and c.model like ?";
            queryList.add("%" + queryModel + "%");
        }
        sql += " ORDER BY r.order_time DESC";
        sql += " limit ?,?";
        int offset = (orderQuery.getPage() - 1) * orderQuery.getLimit();
        queryList.add(offset);
        queryList.add(orderQuery.getLimit());
        List<OrderVo> list = template.query(sql, new BeanPropertyRowMapper<>(OrderVo.class), queryList.toArray());
        return list;
    }

    @Override
    public void deleteById(Integer id) {
        template.update("delete from rental_order where id=?", id);
    }

    @Override
    public void add(Order order) {
        template.update("INSERT INTO rental_order (car_id, user_id, rental_start_date, rental_end_date, total_cost, order_number) VALUES (?,?,?,?,?,?)",
                order.getCarId(), order.getUserId(), order.getRentalStartDate(), order.getRentalEndDate(), order.getTotalCost(), order.getOrderNumber());
    }

    @Override
    public OrderVo selectById(Integer id) {
        return template.queryForObject("SELECT\n" +
                "  r.id AS id,\n" +
                "  r.order_number AS orderNumber,\n" +
                "  c.model AS model,\n" +
                "  c.image AS image,\n" +
                "  c.license_plate AS licensePlate,\n" +
                "  u.username AS username,\n" +
                "  r.rental_start_date AS rentalStartDate,\n" +
                "  r.rental_end_date AS rentalEndDate,\n" +
                "  r.order_time AS orderTime,\n" +
                "  r.total_cost AS totalCost \n" +
                "FROM\n" +
                "  rental_order AS r\n" +
                "  JOIN car AS c ON r.car_id = c.id\n" +
                "  JOIN USER AS u ON r.user_id = u.id \n" +
                "WHERE\n" +
                "  r.id =?", new BeanPropertyRowMapper<>(OrderVo.class), id);
    }

    @Override
    public void update(Order order) {
        template.update("update rental_order set rental_start_date=?, rental_end_date=?, total_cost=? where id=?"
                , order.getRentalStartDate(), order.getRentalEndDate(), order.getTotalCost(), order.getId());
    }

    @Override
    public void updateOrderStatus(Integer id, Integer orderStatus) {
        template.update("update rental_order set order_status=? where id=?", orderStatus, id);
    }

    @Override
    public List<Order> selectOrderNumber() {
        return template.query("select * from rental_order where order_status=1", new BeanPropertyRowMapper<>(Order.class));
    }

    @Override
    public void updateOrdersStatus(Integer rentalId) {
        template.update("update rental_order set order_status=0 where id=?", rentalId);
    }

    @Override
    public Integer totalCost() {
        Integer count = template.queryForObject("select count(*) from rental_order", Integer.class);
        return count;
    }

    @Override
    public List<OrderCountVo> selectTotalOrder() {
        return template.query("SELECT DATE(order_time) AS orderDate, COUNT(*) AS count\n" +
                "FROM rental_order\n" +
                "WHERE MONTH(order_time) = 5 AND YEAR(order_time) = YEAR(CURRENT_DATE)\n" +
                "GROUP BY DATE(order_time)\n" +
                "ORDER BY orderDate;", new BeanPropertyRowMapper<>(OrderCountVo.class));
    }

    @Override
    public List<OrderVo> selectOrder(Integer userId) {
        List<OrderVo> list = template.query("SELECT \n" +
                "    r.id AS id,\n" +
                "    r.order_number AS orderNumber,\n" +
                "    c.model AS model,\n" +
                "    c.license_plate AS licensePlate,\n" +
                "    c.image AS image,\n" +
                "    u.username AS username,\n" +
                "    r.rental_start_date AS rentalStartDate,\n" +
                "    r.rental_end_date AS rentalEndDate,\n" +
                "    r.order_status AS orderStatus,\n" +
                "    r.total_cost AS totalCost,\n" +
                "    r.order_time AS orderTime\n" +
                "FROM rental_order AS r\n" +
                "JOIN car AS c ON r.car_id = c.id\n" +
                "JOIN user AS u ON r.user_id = u.id where u.id=?", new BeanPropertyRowMapper<>(OrderVo.class), userId);
        return list;
    }
}
